package com.example.bootdemo5.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.bootdemo5.common.CommonResult;
import com.example.bootdemo5.pojo.Employee;
import com.example.bootdemo5.service.IEmployeeService;
import com.example.bootdemo5.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@Api(value = "雇员接口",tags ="雇员接口" )
@RestController
public class EmployeeController {
    @Autowired
    IEmployeeService  employeeService;


    /**
     * /employees?id=1&pageNum=1&password=123456
     * @return
     */
    @ApiOperation("雇员列表查询")
    @GetMapping("/employees")
    public CommonResult  queryList(@RequestParam(value = "pageNum",defaultValue = "1",required = false) Integer pageNum,
                                   @RequestParam(value = "pageSize",defaultValue = "5",required = false) Integer pageSize){
        IPage<Employee>  page  =new Page(pageNum,pageSize);
        employeeService.page(page);
        return  CommonResult.SUCCESS("查询雇员列表成功",page);
    }


    @ApiOperation("雇员关联部门列表查询")
    @GetMapping("/employees/dept")
    public CommonResult  queryPageList(@RequestParam(value = "  pageNum",defaultValue = "1",required = false) Integer pageNum,
                                   @RequestParam(value = "pageSize",defaultValue = "5",required = false) Integer pageSize){
        IPage<Employee>  page  =new Page(pageNum,pageSize);
        employeeService.queryByPage(page);
        return  CommonResult.SUCCESS("查询雇员列表且关联部门成功",page);
    }




    @ApiOperation("根据雇员编号查询")
    @GetMapping("/employees/{id}")
    public CommonResult  queryById(@PathVariable("id") Long   id){
        Employee employee  =employeeService.getById(id);
        return  CommonResult.SUCCESS("查询雇员成功",employee);
    }









    @PostMapping("/employees")
    public  CommonResult   insert (@RequestBody  Employee  employee){
        employeeService.save(employee);
        return  CommonResult.SUCCESS("插入成功",employee);
    }


    @PutMapping ("/employees/{id}")
    public  CommonResult   update ( @PathVariable("id")Long  id, @RequestBody  Employee  employee){
        employee.setId(id);
        employeeService.updateById(employee);
        return  CommonResult.SUCCESS("更新雇员成功");
    }
    @ApiOperation("根据雇员编号修改密码")
    @PatchMapping("/employees/password")
    public  CommonResult   updatePassword(@RequestBody UserVo  userVo){
        UpdateWrapper<Employee>  updateWrapper =new UpdateWrapper<>();
        updateWrapper.set("password",userVo.getPassword());
        updateWrapper.eq("id",userVo.getId());
        employeeService.update(updateWrapper);
        return CommonResult.SUCCESS("修改密码成功");
    }


    @ApiOperation("多条件查询雇员信息")
    @PostMapping("/employees/condition")
    public  CommonResult  queryByCondition(@RequestBody Employee  employee){
        QueryWrapper<Employee>  wrapper  =Wrappers.query();
        wrapper.eq(!Objects.isNull(employee.getEmail()),"email",employee.getEmail());
        wrapper.eq(!Objects.isNull(employee.getUsername()),"username",employee.getUsername());
        wrapper.eq(!Objects.isNull(employee.getName()),"name",employee.getName());
        List<Employee>   employees  =  employeeService.list(wrapper);
        return CommonResult.SUCCESS("多条件查询成功",employees);
    }



    @ApiOperation("多条件查询雇员信息2")
    @PostMapping("/employees/condition2")
    public  CommonResult  queryByCondition2(@RequestBody Employee  employee){
        QueryWrapper<Employee>  wrapper  =Wrappers.query(employee);
        List<Employee>   employees  =  employeeService.list(wrapper);
        return CommonResult.SUCCESS("多条件查询成功",employee);
    }




    @ApiOperation("多条件查询雇员信息3")
    @PostMapping("/employees/condition3")
    public  CommonResult  queryByCondition3(@RequestBody Employee  employee){
        QueryWrapper<Employee>  wrapper  =Wrappers.query(employee);
        List<Employee>   employees  =  employeeService.list(wrapper);
        return CommonResult.SUCCESS("多条件查询成功",employee);
    }



    @ApiOperation("多条件查询雇员信息4")
    @PostMapping("/employees/condition4")
    public  CommonResult  queryByCondition4(@RequestBody Employee  employee){
        QueryWrapper<Employee>  wrapper  =Wrappers.query(employee);
        List<Employee>   emps  =  employeeService.list(wrapper);
        return CommonResult.SUCCESS("多条件查询成功",emps);
    }



}
